package com.gpb.common.method;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * 获取url参数
 * @author Poppy Kwoks
 * @dateTime May 11, 2014  9:28:12 AM
 */
public class ParamMethod implements TemplateMethodModel {
	@Autowired
	private  HttpServletRequest request;
	public Object exec(List arg0) throws TemplateModelException {
		if(request==null){
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		}
		try {
			if(arg0.size()>0){
				if(arg0.size()>1){
					if("int".equals(arg0.get(1))){
						return Integer.parseInt(request.getParameter(arg0.get(0).toString()));
					}else if("double".equals(arg0.get(1))){
						return Double.parseDouble(request.getParameter(arg0.get(0).toString()));
					}
				}
				return request.getParameter(arg0.get(0).toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
